package cn.kgc.vue.controller;

import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.StudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2023/10/31
 * @description
 */
@RestController
@RequestMapping("student")
@CrossOrigin
public class StudentController {


    @Resource
    private StudentService studentService;

    @GetMapping("page")
    public Map  getPage(Integer page,Integer limit){


        Page<Student> studentPage = new Page<>(page, limit);
        studentService.page(studentPage);

        List<Student> records = studentPage.getRecords();
        long total = studentPage.getTotal();

        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","查询成功");
        result.put("data",records);
        result.put("total",total);

        return  result;
    }

    // 添加学生信息
    @PostMapping
    public Map addStudent(@RequestBody Student student){

        studentService.save(student);

        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","添加成功");
        return result;
    }


    @GetMapping("{id}")
    public Map  getById(@PathVariable("id") Integer id){

        Student student = studentService.getById(id);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("data",student);

        return result;
    }


    // 修改学生信息
    @PutMapping
    public Map editStudent(@RequestBody Student student){

        studentService.updateById(student);

        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","更新成功");
        return result;
    }

    // 删除学生信息
    @DeleteMapping("{id}")
    public Map deleteStudent( @PathVariable("id") Integer id){

        studentService.removeById(id);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","删除成功");
        return result;
    }

}
